var { ReturnModel } = require('../libs/resmodel');
var { put,put_single, getImgURL, del, test_face, putMult, initimg, initWaterMark, fileUpload } = require('../services/ossfile');

/**
 * 上传文件-单个
 * @api {post} /ossfile/upload 上传文件-单个
 * @apiDescription 上传文件至阿里云OSS
 * @apiName upload
 * @apiParam (参数) {String} pcode 用户code
 * @apiParam (参数) {String} type 文件类型  头像、个人相册、实名认证、工作认证、学历认证、自我描述、家庭背景、理想另一半、爱情观、兴趣爱好、单身原因
 * @apiParam (参数) {File} file  文件对象
 * @apiSampleRequest /ossfile/upload
 * @apiGroup ossfile-文件操作
 * @apiVersion 1.0.0
 * @apiParamExample {json} Request-Example:
 *     {
 *         "pcode": "290464549598",// 用户code
 *         "type": "头像", // 头像、个人相册、实名认证、工作认证、学历认证、自我描述、家庭背景、理想另一半、爱情观、兴趣爱好、单身原因
 *         "file": { "path": "xxxx", "name": "test.jpg","Content-Length":12000,size: ....},// 文件信息
 *     }
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 *   {
 *      "data": {
 *         "url": "http://redspider.oss-cn-shenzhen.aliyuncs.com/redspider_v1.0/test.jpg",// 文件下载地址
 *         "name": "test.jpg",// 文件名
 *         "objectname": "test.jpg",// 阿里云文件名
 *       },
 *      "msg": "上传成功",
 *      "code": 0
 *   }
 */
async function upload(req, res) {
    let result = await put(req);
    res.json(new ReturnModel(result));
}
 async function upload_single(req, res) {
    let result = await put_single(req);
    res.json(new ReturnModel(result));
}
/**
 * 简历上传-公众号
 * @api {post} /ossfile/file_uoload 简历上传-公众号
 * @apiDescription 简历上传-公众号
 * @apiName file_uoload
 * @apiParam (参数) {File} files  文件对象数组,只取第一个文件
 * @apiSampleRequest /ossfile/file_uoload
* @apiGroup API-公众号
 * @apiVersion 1.0.0
 * @apiParamExample {json} Request-Example:
 *     {
 *         "files": [{ "path": "xxxx", "name": "test.jpg","Content-Length":12000,size: ....}]// 文件信息
 *     }
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 *   {
 *      "data": "http://redspider.oss-cn-shenzhen.aliyuncs.com/redspider_v1.0/test.jpg",// 文件下载地址
 *      "msg": "上传成功",
 *      "code": 0
 *   }
 */
 async function file_uoload(req, res) {
    let result = await fileUpload(req);
    res.json(new ReturnModel(result));
}
/**
 * 上传文件-批量
 * @api {post} /ossfile/uploadMult 上传文件-批量
 * @apiDescription 上传文件至阿里云OSS
 * @apiName uploadMult
 * @apiParam (参数) {File} files  文件对象
 * @apiSampleRequest /ossfile/uploadMult
 * @apiGroup ossfile-文件操作
 * @apiVersion 1.0.0
 * @apiParamExample {json} Request-Example:
 *     {
 *         "files": [{ "path": "xxxx", "name": "test.jpg","Content-Length":12000,size: ....}],// 文件信息
 *     }
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 *   {
 *      "data": [{
 *         "url": "http://redspider.oss-cn-shenzhen.aliyuncs.com/redspider_v1.0/test.jpg",// 文件下载地址
 *         "name": "test.jpg",// 文件名
 *         "objectname": "test.jpg",// 阿里云文件名
 *       }],
 *      "msg": "上传成功",
 *      "code": 0
 *   }
 */
 async function uploadMult(req, res) {
    let result = await putMult(req);
    res.json(new ReturnModel(result));
}
/**
 * 删除文件
 * @api {post} /ossfile/delFile 删除文件
 * @apiDescription 删除文件
 * @apiName delFile
 * @apiParam (参数) {String} objectname 阿里云上文件名称
 * @apiSampleRequest /ossfile/delFile
 * @apiGroup ossfile-文件操作
 * @apiVersion 1.0.0
 */
 async function delFile(req, res) {
    let result = await del(req.body);
    res.json(new ReturnModel(result));
}
/**
* 获取图片网络地址
* @api {post} /ossfile/getImgURL 获取图片网络地址
* @apiDescription 获取图片网络地址
* @apiName getImgURL
* @apiParam (参数) {String} objectname 阿里云上文件名称
* @apiParam (参数) {String} width 将图缩略成指定宽度（默认为100），高度按比例处理
* @apiSampleRequest /ossfile/getImgURL
* @apiGroup ossfile-文件操作
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*     {
*       "objectname": "微信图片_20220319102741.jpg",
*       "width": 500
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "data": "https://redspider.oss-cn-shenzhen.aliyuncs.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20220319102741.jpg?Expires=1648971301&OSSAccessKeyId=TMP.3KjiSNC5fGQKtw6bkP72vpWMfQhsdkwaXki7nWBm9mg5Cqk5ACvHsfibgXLNSPZFeYy5TeSYWL8zbYJYkNNqCC2m7UQXAe&Signature=atNbjhdUEDLn165Zp%2B9I1V2d%2BIk%3D",
*      "msg": "获取成功",
*      "code": 0
*   }
*/
async function getImgUrl(req, res) {
    let result = await getImgURL(req.body);
    res.json(new ReturnModel(result));
}

async function test2(req, res) {
    let result = await test_face();
    res.json(new ReturnModel(result));
}

async function initpeopleHead(req, res){
    res.json(new ReturnModel(initimg()));
}

async function initwatermark(req, res){
    res.json(new ReturnModel(await initWaterMark()));
}

module.exports = function (app) {
    let base_url = '/api/ossfile';

    app.post(base_url + '/upload', upload);
    app.post(base_url + '/upload_single', upload_single);
    app.post(base_url + '/file_uoload', file_uoload);
    app.post(base_url + '/getImgUrl', getImgUrl);
    app.post(base_url + '/delFile', delFile);
    app.post(base_url + '/test_02', test2);
    app.post(base_url + '/uploadMult', uploadMult);
    app.post(base_url + '/initimg', initpeopleHead);
    app.post(base_url + '/initwatermark',initwatermark)
}
